<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="故障类型名称"  prop="faultTypeName">
              <a-input v-model="model.faultTypeName" placeholder="请输入故障类型名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障类型编号"  prop="faultTypeCode">
              <a-input v-model="model.faultTypeCode" placeholder="请输入故障类型编号"  ></a-input>
            </a-form-model-item>
          </a-col>          
          <a-col :span="24">
            <a-form-model-item label="报警级别" prop="alarmLevel">
              <a-select v-model="model.alarmLevel" placeholder="请选择报警级别" allowClear>
                <a-select-option
                  v-for="item in alarmLevelArr"
                  :value="item.value"
                  :key="item.value"
                  >{{ item.name }}
                  </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障类别" prop="faultCategory">
              <a-select v-model="model.faultCategory" placeholder="请选择故障类别">
                <a-select-option value='DEVICE_FAULT'>设备故障</a-select-option>
                <a-select-option value='PARTS_FAULT'>部件故障</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="关联维护建议" prop="suggestId">
              <a-select v-model="model.suggestId" placeholder="请选择关联维护建议">
                <a-select-option v-for='e in sugList' :key='e.id' :value='e.id'>{{e.title}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障描述" prop="description">
              <a-input type="textarea" v-model="model.description" placeholder="请输入故障描述"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'GdDeviceFaultTypeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        alarmLevelArr: [
          { name: '一级', value: 'ONE' },
          { name: '二级', value: 'TWO' },
          { name: '三级', value: 'THREE' },
        ],
        confirmLoading: false,
        validatorRules: {
          faultTypeName:[{ required: true, message: '请输入故障类型名称', trigger: 'blur' }],
          faultTypeCode:[{ required: true, message: '请输入故障类型编号', trigger: 'blur' }],
          alarmLevel:[{ required: true, message: '请选择报警级别', trigger: 'blur' }],
          faultCategory:[{ required: true, message: '请选择故障类别', trigger: 'blur' }],
          suggestId:[{ required: true, message: '请选择关联维护建议', trigger: 'blur' }],
          description:[{ required: true, message: '请输入故障描述', trigger: 'blur' }],
        },
        url: {
          add: "/fault/gdDeviceFaultType/add",
          edit: "/fault/gdDeviceFaultType/edit",
          queryById: "/fault/gdDeviceFaultType/queryById"
        },
        sugList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));

      this.getSugList()
    },
    methods: {
      getSugList(){
        getAction('/fault/gdDeviceSuggest/listAll').then(res=>{
          if(res&&res.code==200) {
            this.sugList = res.result
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>